package com.zbkj.business.kaoshi.reservation;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.annotation.OpenApiTag;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.BaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.Arrays;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: exam_reservation考试预约
 * Author: jason
 * @create: ${creatTime}
 **/
@Service("reservation")
@Slf4j
@OpenApi
//@DS("prod")
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
@OpenApiTag(value = "考试预约",tag = "${projectName}")
public class ReservationService extends BaseService<ReservationDao, Reservation> implements IService<Reservation> {

    /**
     * list
     */
    @OpenApiTag("查询考试预约列表")
    public Return list(CDO cdoRequest) {
        cdoRequest.clearBlankValue();
//        cdoRequest.pageHandle();
//        List<CDO> pageList = this.baseMapper.getPageList(cdoRequest);
//        int total = this.baseMapper.getPageListCount(cdoRequest);
//        CDO cdoResult=new CDO().set("total",total).set("datalist",pageList);
        return super.getPageList(cdoRequest);
    }
    /**
     * info
     */
    @OpenApiTag("查询考试预约信息")
    public Return info(CDO cdoRequest) {
        Reservation bo = BeanUtil.toBean(cdoRequest, Reservation.class);
        LambdaQueryWrapper<Reservation> lqw = buildQueryWrapper(bo);
        Reservation reservation=super.getOne(lqw);
        return Return.valueOf(reservation);
    }

    @OpenApiTag("根据ID查询考试预约信息")
    public Return queryById(CDO cdoRequest) {
        Reservation reservation=super.getById(cdoRequest.getLongValue("id"));
        return Return.valueOf(reservation);
    }

    private LambdaQueryWrapper<Reservation> buildQueryWrapper(Reservation reservation) {
        LambdaQueryWrapper<Reservation> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Reservation::getId);

        lqw.eq(reservation.getId() != null, Reservation::getId, reservation.getId());
        lqw.like(StringUtils.isNotBlank(reservation.getExamName()), Reservation::getExamName, reservation.getExamName());
        lqw.like(StringUtils.isNotBlank(reservation.getExamPhone()), Reservation::getExamPhone, reservation.getExamPhone());
        lqw.like(StringUtils.isNotBlank(reservation.getExamYear()), Reservation::getExamYear, reservation.getExamYear());
        lqw.like(StringUtils.isNotBlank(reservation.getExamMoon()), Reservation::getExamMoon, reservation.getExamMoon());
        lqw.like(StringUtils.isNotBlank(reservation.getExamSun()), Reservation::getExamSun, reservation.getExamSun());
        lqw.like(StringUtils.isNotBlank(reservation.getExamCircumference()), Reservation::getExamCircumference, reservation.getExamCircumference());
        lqw.like(StringUtils.isNotBlank(reservation.getExamTimeSlotId()), Reservation::getExamTimeSlotId, reservation.getExamTimeSlotId());
        lqw.like(StringUtils.isNotBlank(reservation.getExamTimeSlotName()), Reservation::getExamTimeSlotName, reservation.getExamTimeSlotName());
        lqw.like(StringUtils.isNotBlank(reservation.getExamRoom()), Reservation::getExamRoom, reservation.getExamRoom());
        lqw.like(StringUtils.isNotBlank(reservation.getCreateBy()), Reservation::getCreateBy, reservation.getCreateBy());
        lqw.eq(reservation.getCreateDate() != null, Reservation::getCreateDate, reservation.getCreateDate());
        lqw.like(StringUtils.isNotBlank(reservation.getUpdateBy()), Reservation::getUpdateBy, reservation.getUpdateBy());
        lqw.eq(reservation.getUpdateDate() != null, Reservation::getUpdateDate, reservation.getUpdateDate());
        lqw.like(StringUtils.isNotBlank(reservation.getRemarks()), Reservation::getRemarks, reservation.getRemarks());
        lqw.eq(reservation.getDelFlag() != null, Reservation::getDelFlag, reservation.getDelFlag());
        return lqw;
    }

    /**
     * save
     */
    @Override
    @OpenApiTag("保存考试预约信息")
    public Return save(CDO cdoRequest) {
        cdoRequest.remove("createtime");
        cdoRequest.remove("updatetime");
        Reservation bo = BeanUtil.toBean(cdoRequest, Reservation.class);
        Return ret = this.validEntity(cdoRequest,bo,0);
        if(ret.getCode()!=0){
            return ret;
        }
        super.save(bo);
        return Return.SUCCESS;
    }
    /**
     * update
     */
    @OpenApiTag("更新考试预约信息")
    public Return update(CDO cdoRequest) {
        cdoRequest.remove("createtime");
        cdoRequest.remove("updatetime");
        Reservation bo = BeanUtil.toBean(cdoRequest, Reservation.class);
        Return ret = this.validEntity(cdoRequest,bo,1);
        if(ret.getCode()!=0){
            return ret;
        }
        super.updateById(bo);
        return Return.SUCCESS;
    }

    /**
     * delete
     */
    @OpenApiTag("删除考试预约信息")
    public Return delete(CDO cdoRequest) {
        String[] ids = cdoRequest.getStringValue("ids").split(",");
        boolean b = super.removeBatchByIds(Arrays.asList(ids));
        if(b) {
            return Return.SUCCESS;
        }
        return Return.EXCEPTION;
    }

    private Return validEntity(CDO cdoRequest,Reservation entity,Integer type){
        //TODO 做一些数据校验,如唯一约束
        return Return.SUCCESS;
    }
}
